package com.atguigu.gmall.order.config;

import com.atguigu.gmall.rabbit.constant.MqConst;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;



@Component
public class CancelOrderConfig {



    @Bean
    public Queue delayQueue() {
        // 第一个参数是创建的queue的名字，第二个参数是是否支持持久化
        return new Queue(MqConst.QUEUE_ORDER_CANCEL, true);
    }
    @Bean
    public Queue closePaymentQueue(){
        return QueueBuilder.durable(MqConst.QUEUE_PAYMENT_CLOSE).build();
    }
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL,
                "x-delayed-message", true, false, args);
    }
    //同学们课下可以使用注解的方式 从SpingIOC容器中获取
    @Bean
    public Binding bindingDelay() {
        return BindingBuilder.bind(delayQueue()).
                to(delayExchange()).with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
    @Bean
    public Binding closePaymentBinding(){
        return BindingBuilder.bind(closePaymentQueue())
                .to(delayExchange()).with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }

}
